package kr.couponplay.android.activity.deal.list;

import java.util.List;

import kr.couponplay.android.AppContext;
import kr.couponplay.android.activity.R;
import kr.couponplay.android.activity.deal.DealListActivity;
import kr.couponplay.android.activity.deal.category.CategoryHelper;
import kr.couponplay.android.domain.BeanFactory;
import kr.couponplay.android.domain.DataContext;
import kr.couponplay.android.domain.deal.Deal;
import kr.couponplay.android.domain.deal.DealDao;
import android.annotation.SuppressLint;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.ListView;

@SuppressLint("HandlerLeak") 
public class EndDealListFragment extends DealListFragment {
	static final String KEY_CONTENT = "DealListFragment:New";

	public static DealListFragment newInstance(DealListActivity activity, List<Deal> deals) {
		EndDealListFragment fragment = new EndDealListFragment();
//		fragment.layoutInflater = LayoutInflater.from(activity);
		fragment.deals = deals;
		return fragment;
	}
	
	@Override
	public int getPosition() {
		return 1;
	}
	
	@Override
	public int getDealsSize() {
		return DataContext.lastEndDealSize;
	}	
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		if (deals == null) {
			int mainCategoryId = CategoryHelper.mainCategoryId;
			int subCategoryId = CategoryHelper.subCategoryId;
			int offset = 0;
			deals = BeanFactory.getBean(container.getContext().getApplicationContext(), DealDao.class).selectNewDealList(mainCategoryId, subCategoryId, offset);
		}
		
		View onCreateView = inflater.inflate(R.layout.deal_sub_new, container, false);
		dealListView = (ListView) onCreateView.findViewById(R.id.new_deal_list_view);
		dealListHeaderView = (LinearLayout)inflater.inflate(R.layout.deal_list_row_ad, null);
		dealListFooterView = (LinearLayout)inflater.inflate(R.layout.deal_list_row_footer, null);
		
		drawHeader();
		drawBodyWhenInit();
		drawFooter(container.getContext(), deals.size());
		
		dealListViewAdapter = new DealListViewAdapter(container.getContext(), R.layout.deal_list_row, deals);
		dealListView.setAdapter(dealListViewAdapter);
		dealListView.setOnScrollListener(this);
		dealListView.setOnItemClickListener(this);
		return onCreateView;
	}
	
	@Override
	public void drawBodyWhenInit() {
		if (AppContext.isClearEndDeals) {
			changePosition(0);
			AppContext.isClearEndDeals = false;
		}
	}
	
	public void getDataFromServer(final Context context) {
		isAttachLock = true;
		dataLoadindDialog = ProgressDialog.show(context, null, "데이터 수신중입니다.", true, false);
		final int offset = dealListView.getCount() - dealListView.getHeaderViewsCount() - dealListView.getFooterViewsCount();
		
		Thread thread = new Thread(new Runnable() {
			public void run() {
				int mainCategoryId = CategoryHelper.mainCategoryId;
				int subCategoryId = CategoryHelper.subCategoryId;
				List<Deal> deals = BeanFactory.getBean(context.getApplicationContext(), DealDao.class).selectNewDealList(mainCategoryId, subCategoryId, offset);
				
        		Message msg = Message.obtain();
        		msg.obj = deals;
        		handler.sendMessage(msg);
			}
		});
		thread.start();
	}	
	
	private Handler handler = new Handler() {
	    @SuppressWarnings("unchecked")
		public void handleMessage(Message msg) {
	    	List<Deal> deals = (List<Deal>)msg.obj;
	    	drawBodyWhenMore(deals);
	    	drawFooter(null, deals.size());
	    	isAttachLock = false;
	    	dataLoadindDialog.dismiss();
	    }
	};	
}
